package main

import "fmt"

type Books struct{
    tile string
    name string
    index int
}

func main() {
   var book1 Books
   var book2 = Books{"client", "xiaoli", 4}
   
   book1.tile = "tool"
   book1.name = "xiaoming"
   book1.index = 2
   
   fmt.Println(book1)
   fmt.Println(book2.index)
   printBookName(&book2)
   
   var numbers = make([]int,3,5)
   printSlice(numbers)
   
   var numbers2 []int
   printSlice(numbers2)
   
   
   numbers3 := []int{0,1,2,3,4,5,6,7,8}   
   printSlice(numbers3)
   
   printSlice(numbers3[1:4])
   printSlice(numbers3[:3])
   printSlice(numbers3[5:])
   
   numbers3 = append(numbers3, 9)
   numbers3 = append(numbers3, 10)
   numbers3 = append(numbers3, 11, 12, 13)
   printSlice(numbers3)
   
   numbers4 := make([]int, len(numbers3), cap(numbers3))
   copy(numbers4,numbers3)
   printSlice(numbers4)
   printRangeArray(numbers4)
   
   kvs := map[string]string{"a": "apple", "b": "banana"}
   for k, v := range kvs {
       fmt.Printf("%s -> %s\n", k, v)
   }
   
   testMap()
   
   var i int
   for i =0; i<10; i++{
       fmt.Printf("%d\t", factor(i))
   }
   
   fmt.Println("\n")
   
   typeCast()
}

//array
func printBookName(pBook *Books){
   fmt.Println(pBook.name)
}

func printSlice(x []int){
   fmt.Printf("len=%d cap=%d slice=%v\n",len(x),cap(x),x)
}

func printRangeArray(nums []int){
   for i, num := range nums{
   	fmt.Printf("index=%d value=%d\n",i,num)
   }
}

//Map
func testMap(){
   var cMap map[string]string
   cMap = make(map[string]string)
   cMap["it"] = "it"
   cMap["it1"] = "it1"
   cMap["it2"] = "it2"
   
   for k, v := range cMap{
   	fmt.Printf("%s -> %s\n", k, v)
   }
   
   v, ok := cMap["it"]
   if(ok){
   	fmt.Printf("%s\n", v)
   }else{
   	fmt.Println("nil")
   }
   
   delete(cMap, "it1")
   
   v1, ok1 := cMap["it1"]
   if(ok1){
   	fmt.Printf("%s\n", v1)
   }else{
   	fmt.Println(len(cMap))
   }
}

//di gui
func factor(n int)(int){
   if n < 2{
      return n
   }
   
   return factor(n - 1) + factor(n - 2)
}

//type cast
func typeCast(){
   var sum int = 17
   var count int = 5
   var mean float32
   
   mean = float32(sum)/float32(count)
   fmt.Printf("%f\n", mean)
}

